﻿namespace SageKing.Core.Contracts.MemberPath;

public class PathNode
{
    public readonly string Name;

    public readonly PathNodeType NodeType;

    public PathNode(string name, PathNodeType nodeType)
    {
        Name = name;
        NodeType = nodeType;
    }

    public static PathNode NewMember(string memberName)
    {
        return new(memberName, PathNodeType.Member);
    }

    public static PathNode NewIndex(string indexKey, bool isStringKey)
    {
        return new(indexKey, isStringKey ? PathNodeType.StringIndex : PathNodeType.NumberIndex);
    }

    public override string ToString()
    {
        return NodeType switch
        {
            PathNodeType.Member => Name,
            PathNodeType.StringIndex => $"['{Name.Replace("'", "''")}']",
            PathNodeType.NumberIndex => $"[{Name}]",
            _ => throw new ArgumentOutOfRangeException()
        };
    }
}
